package com.kevin.config;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * UrlPageHandler
 *
 * @author
 * @create 2017-12-21 14:18
 **/
public class RequestHandlerInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(RequestHandlerInterceptor.class);
    /**
     * 启动时注入,方便获取系统配置
     *
     * @param env1
     */
    @Autowired
    private Environment env;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("请求URL:{},请求方式为：{},请求协议：{}，请求参数协议类型：{}，请求URL参数为：{}，远程请求地址:{},远程请求主机：{},本地服务地址：{}", request.getRequestURL(), request.getMethod(), request.getScheme(), JSON.toJSONString(request.getHeader("Accept")), JSON.toJSONString(request.getParameterMap()), request.getRemoteAddr(), request.getRemoteHost(), request.getLocalAddr());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
